package com.ruoyi.project.goods.goods.service.impl;

import java.util.List;

import com.ruoyi.framework.web.domain.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.goods.goods.mapper.GoodsMapper;
import com.ruoyi.project.goods.goods.domain.Goods;
import com.ruoyi.project.goods.goods.service.IGoodsService;
import com.ruoyi.common.utils.text.Convert;

/**
 * 商品Service业务层处理
 * 
 * @author lonsn
 * @date 2020-11-04
 */
@Service
public class GoodsServiceImpl implements IGoodsService 
{
    @Autowired
    private GoodsMapper goodsMapper;

    /**
     * 查询商品
     * 
     * @param id 商品ID
     * @return 商品
     */
    @Override
    public Goods selectGoodsById(Integer id)
    {
        return goodsMapper.selectGoodsById(id);
    }

    /**
     * 查询商品
     *
     * @param barCode 商品条码
     * @return 商品
     */
    @Override
    public Goods selectGoodsByKey(Goods goods) {
        return goodsMapper.selectGoodsByKey(goods);
    }

    /**
     * 查询商品列表
     * 
     * @param goods 商品
     * @return 商品
     */
    @Override
    public List<Goods> selectGoodsList(Goods goods)
    {
        return goodsMapper.selectGoodsList(goods);
    }

    /**
     * 新增商品
     * 
     * @param goods 商品
     * @return 结果
     */
    @Override
    public int insertGoods(Goods goods)
    {
        return goodsMapper.insertGoods(goods);
    }

    /**
     * 批量新增
     *
     * @param list
     * @return
     */
    @Override
    public AjaxResult insertBatchGoods(List<Goods> list,String operName) {
        StringBuffer msg=new StringBuffer();
        try {
            for (Goods goods : list){
                goods.setCreateBy(operName);
            }
            goodsMapper.insertBatchGoods(list);
        }catch (Exception e){
            for (Goods goods : list){
                try {
                    goods.setCreateBy(operName);
                    goodsMapper.insertGoods(goods);
                }catch (Exception ee){
                    msg.append(goods.getStyleNumber()).append(";");
                }
            }
        }
        if(msg.length()>5){
            msg.append("以上款号导入失败。");
            return AjaxResult.error(msg.toString());
        }else {
            return AjaxResult.success("导入成功！");
        }
    }

    /**
     * 修改商品
     * 
     * @param goods 商品
     * @return 结果
     */
    @Override
    public int updateGoods(Goods goods)
    {
        return goodsMapper.updateGoods(goods);
    }

    /**
     * 删除商品对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteGoodsByIds(String ids)
    {
        return goodsMapper.deleteGoodsByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除商品信息
     * 
     * @param id 商品ID
     * @return 结果
     */
    @Override
    public int deleteGoodsById(Integer id)
    {
        return goodsMapper.deleteGoodsById(id);
    }
}
